package com.so206.plugins;

import com.so206.po.*;
import com.so206.service.*;
import com.so206.utils.PayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 支付处理
 */
@Component
public class PaymetProcess {

    @Autowired
    private PayConfigService payConfigService;

    @Autowired
    private AlipayConfigService alipayConfigService;

    @Autowired
    private EPayConfigService ePayConfigService;

    @Autowired
    private CodePayConfigService codePayConfigService;

    @Autowired
    private QQPayConfigService qqPayConfigService;

    @Autowired
    private WxPayConfigService wxPayConfigService;

    @Autowired
    private OrderService orderService;

//    @Autowired
//    private ELPaySService elPaySService;

    /**
     * 处理支付信息 -- 测试接口
     */
    public String switchPayMethod_test(HttpServletRequest request, SystemOrder order, SystemWeb web) throws Exception {
        String header = request.getHeader("user-agent");
        if (order.getPayType() == 2) {//支付宝支付
            if (web.getTestAlipay() == 1) {
                return "/payment/yipay?id=" + switch_conn_test(order, web);
            } else if (web.getTestAlipay() == 0) {
                return "/payment/alipay?id=" + switch_conn_test(order, web) + "&isMobile=" + PayUtils.isMobileDevice(header);
            } else if (web.getTestAlipay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_test(order, web);
            } else if (web.getTestAlipay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_test(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 1) {//微信支付
            if (web.getTestWxpay() == 0) {
                boolean mobileDevice = PayUtils.isMobileDevice(header);
                if (mobileDevice) {
                    if (PayUtils.isInWeChat(header)) {
                        return "/payment/payInWeChat?id=" + switch_conn_test(order, web);
                    } else {
                        return "/payment/wap_wx_pay?id=" + switch_conn_test(order, web);
                    }
                } else {
                    return "/payment/wxspay?id=" + switch_conn_test(order, web);
                }
            } else if (web.getTestWxpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_test(order, web);
            } else if (web.getTestWxpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_test(order, web);
            } else if (web.getTestWxpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_test(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 3) { //qq支付
            if (web.getTestQqpay() == 0) {
                return "/payment/qqpayScanCode?id=" + switch_conn_test(order, web);
            } else if (web.getTestQqpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_test(order, web);
            } else if (web.getTestQqpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_test(order, web);
            } else if (web.getTestQqpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_test(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 4) { //易宝支付
            return "/payment/ybpay?id=" + order.getId();
        }
        return "/payment/switch?id=" + order.getId() + "&ret=test";
    }

    /**
     * 计算支付通道并保存 --测试
     */
    private String switch_conn_test(SystemOrder order, SystemWeb web) {
        SystemPayConfig config = payConfigService.getPayConfig(1L);
        //轮询
        if (config.getLxStatus() == 1) {
            //轮询开启
            Integer type = order.getPayType();
            if (type == 1) { //微信
                Integer testWxpay = web.getTestWxpay();
                if (testWxpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<WxpayConfig> list = wxPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            WxpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<WxpayConfig> list1 = new ArrayList<>();
                            for (WxpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            WxpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 2) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                } else if (testWxpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) { //支付宝
                Integer testAlipay = web.getTestAlipay();
                if (testAlipay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<AlipayConfig> list = alipayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            AlipayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<AlipayConfig> list1 = new ArrayList<>();
                            for (AlipayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            AlipayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 1) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) { //qq
                Integer testQqpay = web.getTestQqpay();
                if (testQqpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<QqpayConfig> list = qqPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            QqpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<QqpayConfig> list1 = new ArrayList<>();
                            for (QqpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            QqpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 3) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            }
        } else {
            //轮询关闭
            Integer type = order.getPayType();
            if (type == 1) {
                Integer testWxpay = web.getTestWxpay();
                if (testWxpay == 0) {
                    order.setPayConnet(config.getDefaultWxpay());
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    order.setPayConnet(config.getDefaultEpayWxpay());
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayWxpay());
                    order.setPayConnetType(1);
                }
//                else if (testWxpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Wxpay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) {
                Integer testAlipay = web.getTestAlipay();
                if (testAlipay == 0) {
                    order.setPayConnet(config.getDefaultAlipay());
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    order.setPayConnet(config.getDefaultEpayAlipay());
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Alipay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) {
                Integer testQqpay = web.getTestQqpay();
                if (testQqpay == 0) {
                    order.setPayConnet(config.getDelaultQqpay());
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    order.setPayConnet(config.getDefaultEpayQqpay());
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Qqpay());
//                    order.setPayConnetType(6);
//                }
            }
        }
        //立减优惠
        if (config.getPaySale() == 1) {
            BigDecimal price = order.getPrice();
            if (price.compareTo(config.getPaySaleStart()) >= 0 && price.compareTo(config.getPaySaleStop()) <= 0) {
                double min = config.getPaySaleMin().doubleValue();
                double max = config.getPaySaleMax().doubleValue();
                BigDecimal db = new BigDecimal(Math.random() * (max - min) + min);
                order.setSaleMonney(db);
            } else {
                order.setSaleMonney(new BigDecimal(0.00));
            }
        } else {
            order.setSaleMonney(new BigDecimal(0.00));
        }
        orderService.updateOrder(order);
        return order.getId().toString();
    }

    /**
     * 处理支付信息 -- 系统接口
     */
    public String switchPayMethod2(HttpServletRequest request, SystemOrder order, SystemWeb web) throws Exception {
        String header = request.getHeader("user-agent");
        if (order.getPayType() == 2) {//支付宝支付
            if (web.getPayToAlipay() == 1) {
                return "/payment/yipay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToAlipay() == 0) {
                return "/payment/alipay?id=" + switch_conn_system(order, web) + "&isMobile=" + PayUtils.isMobileDevice(header);
            } else if (web.getPayToAlipay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToAlipay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_system(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 1) {//微信支付
            if (web.getPayToWxpay() == 0) {
                boolean mobileDevice = PayUtils.isMobileDevice(header);
                if (mobileDevice) {
                    if (PayUtils.isInWeChat(header)) {
                        return "/payment/payInWeChat?id=" + switch_conn_system(order, web);
                    } else {
                        return "/payment/wap_wx_pay?id=" + switch_conn_system(order, web);
                    }
                } else {
                    return "/payment/wxspay?id=" + switch_conn_system(order, web);
                }
            } else if (web.getPayToWxpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToWxpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToWxpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_system(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 3) { //qq支付
            if (web.getPayToQqpay() == 0) {
                return "/payment/qqpayScanCode?id=" + switch_conn_system(order, web);
            } else if (web.getPayToQqpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToQqpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_system(order, web);
            } else if (web.getPayToQqpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_system(order, web);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        }
        return "/payment/switch?id=" + order.getId() + "&ret=sys";
    }

    /**
     * 计算支付通道并保存 --系统接口
     */
    private String switch_conn_system(SystemOrder order, SystemWeb web) {
        SystemPayConfig config = payConfigService.getPayConfig(1L);
        //轮询
        if (config.getLxStatus() == 1) {
            //轮询开启
            Integer type = order.getPayType();
            if (type == 1) { //微信
                Integer testWxpay = web.getPayToWxpay();
                if (testWxpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<WxpayConfig> list = wxPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            WxpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<WxpayConfig> list1 = new ArrayList<>();
                            for (WxpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            WxpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 2) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testWxpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) { //支付宝
                Integer testAlipay = web.getPayToAlipay();
                if (testAlipay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<AlipayConfig> list = alipayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            AlipayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<AlipayConfig> list1 = new ArrayList<>();
                            for (AlipayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            AlipayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 1) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) { //qq
                Integer testQqpay = web.getPayToQqpay();
                if (testQqpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<QqpayConfig> list = qqPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            QqpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<QqpayConfig> list1 = new ArrayList<>();
                            for (QqpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            QqpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 3) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            }
        } else {
            //轮询关闭
            Integer type = order.getPayType();
            if (type == 1) {
                Integer testWxpay = web.getPayToWxpay();
                if (testWxpay == 0) {
                    order.setPayConnet(config.getDefaultWxpay());
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    order.setPayConnet(config.getDefaultEpayWxpay());
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayWxpay());
                    order.setPayConnetType(1);
                }
//                else if (testWxpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Wxpay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) {
                Integer testAlipay = web.getPayToAlipay();
                if (testAlipay == 0) {
                    order.setPayConnet(config.getDefaultAlipay());
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    order.setPayConnet(config.getDefaultEpayAlipay());
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Alipay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) {
                Integer testQqpay = web.getPayToQqpay();
                if (testQqpay == 0) {
                    order.setPayConnet(config.getDelaultQqpay());
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    order.setPayConnet(config.getDefaultEpayQqpay());
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Qqpay());
//                    order.setPayConnetType(6);
//                }
            }
        }
        //立减优惠
        if (config.getPaySale() == 1) {
            BigDecimal price = order.getPrice();
            if (price.compareTo(config.getPaySaleStart()) >= 0 && price.compareTo(config.getPaySaleStop()) <= 0) {
                double min = config.getPaySaleMin().doubleValue();
                double max = config.getPaySaleMax().doubleValue();
                BigDecimal db = new BigDecimal(Math.random() * (max - min) + min);
                order.setSaleMonney(db);
            } else {
                order.setSaleMonney(new BigDecimal(0.00));
            }
        } else {
            order.setSaleMonney(new BigDecimal(0.00));
        }
        orderService.updateOrder(order);
        return order.getId().toString();
    }

    /**
     * 处理支付信息 -- API接口
     */
    public String switchPayMethod_API(HttpServletRequest request, SystemOrder order, SystemUser user) throws Exception {
        String header = request.getHeader("user-agent");
        if (order.getPayType() == 2) {//支付宝支付
            if (user.getPayAlipay() == 1) {
                return "/payment/yipay?id=" + switch_conn_api(order, user);
            } else if (user.getPayAlipay() == 0) {
                return "/payment/alipay?id=" + switch_conn_api(order, user) + "&isMobile=" + PayUtils.isMobileDevice(header);
            } else if (user.getPayAlipay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_api(order, user);
            } else if (user.getPayAlipay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_api(order, user);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 1) {//微信支付
            if (user.getPayWxpay() == 0) {
                boolean mobileDevice = PayUtils.isMobileDevice(header);
                if (mobileDevice) {
                    if (PayUtils.isInWeChat(header)) {
                        return "/payment/payInWeChat?id=" + switch_conn_api(order, user);
                    } else {
                        return "/payment/wap_wx_pay?id=" + switch_conn_api(order, user);
                    }
                } else {
                    return "/payment/wxspay?id=" + switch_conn_api(order, user);
                }
            } else if (user.getPayWxpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_api(order, user);
            } else if (user.getPayWxpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_api(order, user);
            } else if (user.getPayWxpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_api(order, user);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        } else if (order.getPayType() == 3) { //qq支付
            if (user.getPayQqpay() == 0) {
                return "/payment/qqpayScanCode?id=" + switch_conn_api(order, user);
            } else if (user.getPayQqpay() == 1) {
                return "/payment/yipay?id=" + switch_conn_api(order, user);
            } else if (user.getPayQqpay() == 2) {
                return "/payment/code_pay?id=" + switch_conn_api(order, user);
            } else if (user.getPayQqpay() == 3) {
                return "/payment/el_pay?id=" + switch_conn_api(order, user);
            } else {
                throw new Exception("当前接口已关闭支付功能,请稍后重试!");
            }
        }
        return "/payment/switch?id=" + order.getId() + "&ret=api";
    }

    /**
     * 计算支付通道并保存 --API接口
     */
    private String switch_conn_api(SystemOrder order, SystemUser user) {
        SystemPayConfig config = payConfigService.getPayConfig(1L);
        //轮询
        if (config.getLxStatus() == 1) {
            //轮询开启
            Integer type = order.getPayType();
            if (type == 1) { //微信
                Integer testWxpay = user.getPayWxpay();
                if (testWxpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<WxpayConfig> list = wxPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            WxpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<WxpayConfig> list1 = new ArrayList<>();
                            for (WxpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            WxpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 2) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testWxpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 2) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 2) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) { //支付宝
                Integer testAlipay = user.getPayAlipay();
                if (testAlipay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<AlipayConfig> list = alipayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            AlipayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<AlipayConfig> list1 = new ArrayList<>();
                            for (AlipayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            AlipayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 1) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 1) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 1) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) { //qq
                Integer testQqpay = user.getPayQqpay();
                if (testQqpay == 0) {
                    if (order.getPayConnet() == 0) {
                        List<QqpayConfig> list = qqPayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            QqpayConfig model = list.get(new Random().nextInt(list.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<QqpayConfig> list1 = new ArrayList<>();
                            for (QqpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    list1.add(wxpayConfig);
                                }
                            }
                            QqpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    if (order.getPayConnet() == 0) {
                        List<EpayConfig> list = ePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig epayConfig : list) {
                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
                                    list1.add(epayConfig);
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<EpayConfig> list1 = new ArrayList<>();
                            for (EpayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            EpayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    if (order.getPayConnet() == 0) {
                        List<CodepayConfig> list = codePayConfigService.find_all_conn(1);
                        if (config.getLxMode() == 0) {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig codepayConfig : list) {
                                if (codepayConfig.getPayType() == 0 | codepayConfig.getPayType() == 3) {
                                    list1.add(codepayConfig);
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        } else {
                            ArrayList<CodepayConfig> list1 = new ArrayList<>();
                            for (CodepayConfig wxpayConfig : list) {
                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
                                        list1.add(wxpayConfig);
                                    }
                                }
                            }
                            CodepayConfig model = list1.get(new Random().nextInt(list1.size()));
                            order.setPayConnet(model.getId());
                        }
                    }
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    if (order.getPayConnet() == 0) {
//                        List<ElpayConfig> list = elPaySService.find_all_conn(1);
//                        if (config.getLxMode() == 0) {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig epayConfig : list) {
//                                if (epayConfig.getPayType() == 0 | epayConfig.getPayType() == 3) {
//                                    list1.add(epayConfig);
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        } else {
//                            ArrayList<ElpayConfig> list1 = new ArrayList<>();
//                            for (ElpayConfig wxpayConfig : list) {
//                                if (wxpayConfig.getMinPayMoney().compareTo(order.getPrice()) <= 0
//                                        && wxpayConfig.getMaxPayMoney().compareTo(order.getPrice()) >= 0) {
//                                    if (wxpayConfig.getPayType() == 0 | wxpayConfig.getPayType() == 3) {
//                                        list1.add(wxpayConfig);
//                                    }
//                                }
//                            }
//                            ElpayConfig model = list1.get(new Random().nextInt(list1.size()));
//                            order.setPayConnet(model.getId());
//                        }
//                    }
//                    order.setPayConnetType(6);
//                }
            }
        } else {
            //轮询关闭
            Integer type = order.getPayType();
            if (type == 1) {
                Integer testWxpay = user.getPayWxpay();
                if (testWxpay == 0) {
                    order.setPayConnet(config.getDefaultWxpay());
                    order.setPayConnetType(2);
                } else if (testWxpay == 1) {
                    order.setPayConnet(config.getDefaultEpayWxpay());
                    order.setPayConnetType(0);
                } else if (testWxpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayWxpay());
                    order.setPayConnetType(1);
                }
//                else if (testWxpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Wxpay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 2) {
                Integer testAlipay = user.getPayAlipay();
                if (testAlipay == 0) {
                    order.setPayConnet(config.getDefaultAlipay());
                    order.setPayConnetType(4);
                } else if (testAlipay == 1) {
                    order.setPayConnet(config.getDefaultEpayAlipay());
                    order.setPayConnetType(0);
                } else if (testAlipay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testAlipay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Alipay());
//                    order.setPayConnetType(6);
//                }
            } else if (type == 3) {
                Integer testQqpay = user.getPayQqpay();
                if (testQqpay == 0) {
                    order.setPayConnet(config.getDelaultQqpay());
                    order.setPayConnetType(3);
                } else if (testQqpay == 1) {
                    order.setPayConnet(config.getDefaultEpayQqpay());
                    order.setPayConnetType(0);
                } else if (testQqpay == 2) {
                    order.setPayConnet(config.getDefaultCodepayQq());
                    order.setPayConnetType(1);
                }
//                else if (testQqpay == 3) {
//                    order.setPayConnet(config.getDefaultEpay2Qqpay());
//                    order.setPayConnetType(6);
//                }
            }
        }
        //立减优惠
        if (config.getPaySale() == 1) {
            BigDecimal price = order.getPrice();
            if (price.compareTo(config.getPaySaleStart()) >= 0 && price.compareTo(config.getPaySaleStop()) <= 0) {
                double min = config.getPaySaleMin().doubleValue();
                double max = config.getPaySaleMax().doubleValue();
                BigDecimal db = new BigDecimal(Math.random() * (max - min) + min);
                order.setSaleMonney(db);
            } else {
                order.setSaleMonney(new BigDecimal("0.00"));
            }
        } else {
            order.setSaleMonney(new BigDecimal("0.00"));
        }
        orderService.updateOrder(order);
        return order.getId().toString();
    }

}
